package com.uestc.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uestc.entity.ResearchPlatform;
import com.uestc.mapper.AboutUsDetailMapper;
import com.uestc.mapper.AboutUsMapper;
import com.uestc.mapper.ContactUsMapper;
import com.uestc.mapper.LinkMapper;
import com.uestc.mapper.NewsCategoryMapper;
import com.uestc.mapper.NewsMapper;
import com.uestc.mapper.PartMapper;
import com.uestc.mapper.ServicesMapper;
import com.uestc.mapper.SliderMapper;
import com.uestc.mapper.Training1Mapper;
import com.uestc.mapper.TrainingLinkMapper;
import com.uestc.mapper.TrainingListMapper;
import com.uestc.mapper.TrainingMapper;
import com.uestc.pojo.AboutUsDetail;
import com.uestc.pojo.AboutUsDetailExample;
import com.uestc.pojo.ContactUsExample;
import com.uestc.pojo.LinkExample;
import com.uestc.pojo.News;
import com.uestc.pojo.NewsExample;
import com.uestc.pojo.NewsExample.Criteria;
import com.uestc.repository.ResearchPlatformCategoryRepository;
import com.uestc.repository.ResearchPlatformRepository;
import com.uestc.pojo.Part;
import com.uestc.pojo.PartExample;
import com.uestc.pojo.SliderExample;
import com.uestc.pojo.Training;
import com.uestc.pojo.Training1;
import com.uestc.pojo.TrainingLinkExample;
import com.uestc.pojo.TrainingList;
import com.uestc.pojo.TrainingListExample;
import com.uestc.service.IPageService;

@Controller
@RequestMapping(value = "/")
public class PageController {
	@Autowired
	private NewsMapper newsMapper;
	@Autowired
	private SliderMapper sliderMapper;
	@Autowired
	private NewsCategoryMapper categoryMapper;
	@Autowired
	private ServicesMapper servicesMapper;
	@Autowired
	private AboutUsDetailMapper aboutUsDetailMapper;
	@Autowired
	private AboutUsMapper aboutUsMapper;
	@Autowired
	private ContactUsMapper contactUsMapper;
	@Autowired
	private IPageService pageService;
	@Autowired
	private LinkMapper linkMapper;
	@Autowired
	private PartMapper partMapper;
	@Autowired
	private TrainingMapper trainingMapper;
	@Autowired
	private Training1Mapper training1Mapper;
	@Autowired
	private TrainingListMapper trainingListMapper;
	@Autowired
	private TrainingLinkMapper trainingLinkMapper;
	@Autowired
	private ResearchPlatformRepository researchPlatformRepository;
	@Autowired
	private ResearchPlatformCategoryRepository researchPlatformCategoryRepository;

	@GetMapping(value = { "/", "/index" })
	public String index(Model model, HttpServletRequest req) {
		model.addAttribute("focusNews", pageService.selectfocusNewsForIndex());
		model.addAttribute("newsMap", pageService.selectNewsForIndex());
		SliderExample example = new SliderExample();
		com.uestc.pojo.SliderExample.Criteria c = example.createCriteria();
		c.andTypeEqualTo(0);
		model.addAttribute("sliderList", sliderMapper.selectByExample(example));
		PageHelper.startPage(0, 10);
		PartExample e = new PartExample();
		e.setOrderByClause("create_time desc");
		List<Part> list = partMapper.selectByExample(e);
		PageInfo<Part> pageInfo = new PageInfo<>(list);
		model.addAttribute("partList", list);
		return "index";
	}

	@GetMapping(value = "/detail/{categoryId}/{id}")
	public String detail(Model model, @PathVariable(required = true) Long categoryId,
			@PathVariable(required = true) String id) {
		// 轮播图
		SliderExample example1 = new SliderExample();
		com.uestc.pojo.SliderExample.Criteria c1 = example1.createCriteria();
		c1.andTypeEqualTo(1);
		model.addAttribute("sliderList", sliderMapper.selectByExample(example1));
		model.addAttribute("categoryInfo", categoryMapper.selectByPrimaryKey(categoryId));
		model.addAttribute("newInfo", newsMapper.selectByPrimaryKey(id));
		LinkExample example = new LinkExample();
		com.uestc.pojo.LinkExample.Criteria c = example.createCriteria();
		c.andParentIdEqualTo(id);
		model.addAttribute("linkList", linkMapper.selectByExample(example));
		model.addAttribute("next", pageService.selectNext(categoryId, id));
		model.addAttribute("previous", pageService.selectPrevious(categoryId, id));
		return "pages/detail";
	}

	@GetMapping(value = "/news/{id}")
	public String news(Model model, @PathVariable(required = true) Long id,
			@RequestParam(name = "pageNum", defaultValue = "0") Integer pageNum) {
		// 轮播图
		SliderExample example1 = new SliderExample();
		com.uestc.pojo.SliderExample.Criteria c1 = example1.createCriteria();
		c1.andTypeEqualTo(1);
		model.addAttribute("sliderList", sliderMapper.selectByExample(example1));
		model.addAttribute("categoryInfo", categoryMapper.selectByPrimaryKey(id));
		PageHelper.startPage(pageNum, 10);
		NewsExample example = new NewsExample();
		example.setOrderByClause("create_time desc");
		Criteria c = example.createCriteria();
		c.andCategoryIdEqualTo(id);
		List<News> list = newsMapper.selectByExample(example);
		PageInfo<News> pageInfo = new PageInfo<>(list);
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("newsList", list);
		return "pages/news";
	}

	@GetMapping(value = "/party")
	public String party(Model model, @RequestParam(name = "pageNum", defaultValue = "0") Integer pageNum) {
		// 轮播图
		SliderExample example1 = new SliderExample();
		com.uestc.pojo.SliderExample.Criteria c = example1.createCriteria();
		c.andTypeEqualTo(1);
		model.addAttribute("sliderList", sliderMapper.selectByExample(example1));
		PageHelper.startPage(pageNum, 10);
		PartExample example = new PartExample();
		example.setOrderByClause("create_time desc");
		List<Part> list = partMapper.selectByExample(example);
		PageInfo<Part> pageInfo = new PageInfo<>(list);
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("partList", list);
		return "pages/party";
	}

	@GetMapping(value = "/partydetail/{id}")
	public String partydetail(Model model, @PathVariable(required = true) Long id) {
		// 轮播图
		SliderExample example = new SliderExample();
		com.uestc.pojo.SliderExample.Criteria c = example.createCriteria();
		c.andTypeEqualTo(1);
		model.addAttribute("sliderList", sliderMapper.selectByExample(example));
		model.addAttribute("partyInfo", partMapper.selectByPrimaryKey(id));
		model.addAttribute("next", pageService.selectPartyNext(id));
		model.addAttribute("previous", pageService.selectPartyPrevious(id));
		return "pages/partydetail";
	}

	@GetMapping(value = "/services/{id}")
	public String services(Model model, @PathVariable(required = true) Integer id) {
		// 轮播图
		SliderExample example = new SliderExample();
		com.uestc.pojo.SliderExample.Criteria c = example.createCriteria();
		c.andTypeEqualTo(1);
		model.addAttribute("sliderList", sliderMapper.selectByExample(example));
		model.addAttribute("serviceInfo", servicesMapper.selectByPrimaryKey(id));
		return "pages/service";
	}

	@GetMapping(value = "/training/{parentId}/{id}")
	public String training(Model model, @PathVariable(required = true) Integer parentId, @PathVariable Integer id,
			@RequestParam(name = "pageNum", defaultValue = "0") Integer pageNum) {
		// 轮播图
		SliderExample example = new SliderExample();
		com.uestc.pojo.SliderExample.Criteria c = example.createCriteria();
		c.andTypeEqualTo(1);
		model.addAttribute("sliderList", sliderMapper.selectByExample(example));
		model.addAttribute("trainingInfo", trainingMapper.selectByPrimaryKey(parentId));
		// 博士后创新实践基地
		if (parentId == 0) {
			Training trainingInfo = (Training) model.asMap().get("trainingInfo");
			Training1 training = training1Mapper.selectByPrimaryKey(id);
			trainingInfo.setTrainingName(training.getName());
			trainingInfo.setChildrenId(training.getId());
			model.addAttribute("trainingInfo", trainingInfo);
			if (id != 1) {
				// 制度办法和项目展示
				PageHelper.startPage(pageNum, 10);
				TrainingListExample e2 = new TrainingListExample();
				e2.setOrderByClause("create_time desc");
				com.uestc.pojo.TrainingListExample.Criteria c2 = e2.createCriteria();
				c2.andParentIdEqualTo(id);
				List<TrainingList> list = trainingListMapper.selectByExample(e2);
				PageInfo<TrainingList> pageInfo = new PageInfo<>(list);
				model.addAttribute("pageInfo", pageInfo);
				model.addAttribute("trainingList", list);
				return "pages/training_list";
			} else {
				trainingInfo.setContent(training.getContent());
				model.addAttribute("trainingInfo", trainingInfo);
			}
		} else {
			Training trainingInfo = (Training) model.asMap().get("trainingInfo");
			trainingInfo.setChildrenId(0);
			model.addAttribute("trainingInfo", trainingInfo);
			return "pages/training";
		}
		return "pages/training";
	}

	@GetMapping(value = "/trainingdetail/{parentId}/{id}")
	public String trainingdetail(Model model, @PathVariable(required = true) Integer parentId,
			@PathVariable(required = true) Integer id) {
		// 轮播图
		SliderExample example = new SliderExample();
		com.uestc.pojo.SliderExample.Criteria c = example.createCriteria();
		c.andTypeEqualTo(1);
		model.addAttribute("sliderList", sliderMapper.selectByExample(example));
		model.addAttribute("partyInfo", training1Mapper.selectByPrimaryKey(parentId));
		model.addAttribute("trainingListInfo", trainingListMapper.selectByPrimaryKey(id));
		model.addAttribute("next", pageService.selectTrainingNext(parentId, id));
		model.addAttribute("previous", pageService.selectTrainingPrevious(parentId, id));
		TrainingLinkExample e2 = new TrainingLinkExample();
		com.uestc.pojo.TrainingLinkExample.Criteria c2 = e2.createCriteria();
		c2.andParentIdEqualTo(id);
		model.addAttribute("linkList", trainingLinkMapper.selectByExample(e2));
		return "pages/trainingdetail";
	}

	@GetMapping(value = "/aboutus/{id}")
	public String aboutus(Model model, @PathVariable(required = true) Integer id,
			@RequestParam(name = "pageNum", defaultValue = "0") Integer pageNum) {
		if (id == 1) {
			// 轮播图
			SliderExample example1 = new SliderExample();
			com.uestc.pojo.SliderExample.Criteria c = example1.createCriteria();
			c.andTypeEqualTo(1);
			model.addAttribute("sliderList", sliderMapper.selectByExample(example1));
			PageHelper.startPage(pageNum, 10);
			AboutUsDetailExample example = new AboutUsDetailExample();
			example.setOrderByClause("create_time desc");
			List<AboutUsDetail> list = aboutUsDetailMapper.selectByExample(example);
			PageInfo<AboutUsDetail> pageInfo = new PageInfo<>(list);
			model.addAttribute("pageInfo", pageInfo);
			model.addAttribute("aboutUsDetails", list);
			return "pages/aboutus2";
		} else {
			// 轮播图
			SliderExample example = new SliderExample();
			com.uestc.pojo.SliderExample.Criteria c = example.createCriteria();
			c.andTypeEqualTo(1);
			model.addAttribute("sliderList", sliderMapper.selectByExample(example));
			model.addAttribute("aboutUsInfo", aboutUsMapper.selectByPrimaryKey(id));
			return "pages/aboutus1";
		}
	}

	@GetMapping(value = "/contactus")
	public String contactus(Model model) {
		// 轮播图
		SliderExample example = new SliderExample();
		com.uestc.pojo.SliderExample.Criteria c = example.createCriteria();
		c.andTypeEqualTo(1);
		model.addAttribute("sliderList", sliderMapper.selectByExample(example));
		ContactUsExample e1 = new ContactUsExample();
		model.addAttribute("contactUs", contactUsMapper.selectByExampleWithBLOBs(e1).get(0));
		return "pages/contactus";
	}

	@PostMapping(value = "/search")
	public String search(Model model, String content) {
		// 轮播图
		SliderExample example1 = new SliderExample();
		com.uestc.pojo.SliderExample.Criteria c1 = example1.createCriteria();
		c1.andTypeEqualTo(1);
		model.addAttribute("sliderList", sliderMapper.selectByExample(example1));
		PageHelper.startPage(0, 10);
		NewsExample example = new NewsExample();
		example.setOrderByClause("create_time desc");
		Criteria c = example.createCriteria();
		c.andTitleLike("%" + content + "%");
		List<News> list = newsMapper.selectByExample(example);
		PageInfo<News> pageInfo = new PageInfo<>(list);
		model.addAttribute("content", content);
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("newsList", list);
		return "pages/search";
	}

	@GetMapping(value = "/search/{content}")
	public String search(Model model, @RequestParam(name = "pageNum", defaultValue = "0") Integer pageNum,
			@PathVariable(required = true) String content) {
		// 轮播图
		SliderExample example1 = new SliderExample();
		com.uestc.pojo.SliderExample.Criteria c1 = example1.createCriteria();
		c1.andTypeEqualTo(1);
		model.addAttribute("sliderList", sliderMapper.selectByExample(example1));
		PageHelper.startPage(pageNum, 10);
		NewsExample example = new NewsExample();
		example.setOrderByClause("create_time desc");
		Criteria c = example.createCriteria();
		c.andTitleLike("%" + content + "%");
		List<News> list = newsMapper.selectByExample(example);
		PageInfo<News> pageInfo = new PageInfo<>(list);
		model.addAttribute("content", content);
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("newsList", list);
		return "pages/search";
	}

	@GetMapping(value = "/research-platform/{id}")
	public String researchPlatform(Model model, @PathVariable(required = true) Long id,
			@RequestParam(name = "pageNum", defaultValue = "0") Integer pageNum) {
		// 轮播图
		SliderExample example1 = new SliderExample();
		com.uestc.pojo.SliderExample.Criteria c1 = example1.createCriteria();
		c1.andTypeEqualTo(1);
		model.addAttribute("sliderList", sliderMapper.selectByExample(example1));
		model.addAttribute("categoryInfo", researchPlatformCategoryRepository.findById(id).get());
		model.addAttribute("categoryList", researchPlatformCategoryRepository.findAll());
		Pageable pageable = new PageRequest(pageNum, 10);
		org.springframework.data.domain.Page<ResearchPlatform> page = researchPlatformRepository.findByCategoryId(id, pageable);
		model.addAttribute("pageInfo", page);
		return "pages/research-platform";
	}
	

	@GetMapping(value = "/research-platform/detail/{categoryId}/{id}")
	public String researchPlatformDetail(Model model, @PathVariable(required = true) Long categoryId,
			@PathVariable(required = true) Long id) {
		// 轮播图
		SliderExample example1 = new SliderExample();
		com.uestc.pojo.SliderExample.Criteria c1 = example1.createCriteria();
		c1.andTypeEqualTo(1);
		model.addAttribute("sliderList", sliderMapper.selectByExample(example1));
		model.addAttribute("categoryInfo", researchPlatformCategoryRepository.findById(categoryId).get());
		model.addAttribute("categoryList", researchPlatformCategoryRepository.findAll());
		ResearchPlatform researchPlatform = researchPlatformRepository.findById(id).get();
		model.addAttribute("pageInfo", researchPlatform);
		Map<String, Object> previous = researchPlatformRepository.getPrevious(categoryId, researchPlatform.getCreateTime());
		if(previous.isEmpty()) {
			previous.put("id", id);
			previous.put("title", researchPlatform.getTitle());
		}
		model.addAttribute("previous", previous);
		Map<String, Object> next = researchPlatformRepository.getNext(categoryId, researchPlatform.getCreateTime());
		if(previous.isEmpty()) {
			previous.put("id", id);
			previous.put("title", researchPlatform.getTitle());
		}
		model.addAttribute("next", next);
		return "pages/research-platform-detail";
	}
}
